<template>
	<view class="default-background">
		<view class="publish-house-page">
			<view class="publish-item-group">
				<view class="item-group">
					<view class="item-input-group">
						<input class="item-input" placeholder="写个标题" v-model="house.title" maxlength="20"/>
					</view>
				</view>
				<view class="item-group">
					<view class="item-input-group">
						<textarea class="item-input" placeholder="说点好玩的" v-model="house.introduce" maxlength="120"/>
					</view>
				</view>
				<view class="room-title">
					<view class="room-title-text">房源信息</view>
					<picker :range="houseRange" @change="houseChange" style="color: #666666; font-size: 16px;">
						<label>{{houseDefault}}</label>
					</picker>
				</view>
				<view class="room-info">
					<view class="room-item">
						<input class="item-input" type="number" v-model="house.room" maxlength="2"/>
						<view class="item-text">房</view>
					</view>
					<view class="room-item">
						<input class="item-input" type="number" v-model="house.parlor" maxlength="2"/>
						<view class="item-text">厅</view>
					</view>
					<view class="room-item">
						<input class="item-input" type="number" v-model="house.lavatory" maxlength="2"/>
						<view class="item-text">卫</view>
					</view>
				</view>
				<view class="item-group">
					<view class="item-input-group">
						<picker @change="districtPickerChange" :value="districtIndex" :range="districtArray" range-key="name" class="item-input">
							<view>{{ districtArray[districtIndex].name }}</view>
						</picker>
					</view>
				</view>
				<view class="item-group">
					<view class="item-input-group">
						<input class="item-input" placeholder="房源详细地址" v-model="house.address" maxlength="25"/>
					</view>
				</view>
				<view class="item-group">
					<view class="item-input-group">
						<input class="item-input" placeholder="价格" v-model="house.price" @input="checkNum" maxlength="10"/>
					</view>
				</view>
				<image v-for="(url, index) in house.picShow" :src="url" class="house-pic" mode="aspectFill" @click="getImg(index)"></image>
				<view class="item-group" style="border-bottom: none;">
					<view class="item-bar">上传图片</view>
					<uni-file-picker :value="filePathsList" :auto-upload="false" file-mediatype="image" mode="grid" class="imgs"
								file-extname="png,jpg" :limit="9" @select="handleSelect" @delete="handleDelete" @success="success" />
				</view>
			</view>
			<view class="page-bottom"></view>
		</view>
		<view class="btn-group">
			<view class="cancelBtn button" @click="deleteHouse">删除</view>
			<view v-if="!house.isDisplay" class="confirmBtn button" @click="display">公开</view>
			<view v-else class="cancelBtn button" @click="cancelDisplay">取消公开</view>
			<view class="confirmBtn button" @click="saveChange">保存</view>
		</view>
	</view>
</template>

<script>
	import config  from "@/config/app.js"
	export default {
		data() {
			return {
				filePathsList: [],
				house: {
					title: "",
					introduce: "",
					address: "",
					room: 0,
					parlor: 0,
					lavatory: 0,
					district: "城厢区",
					type: 0,
					price: 0,
					pic: ""
				},
				houseRange: ["出租", "出售"],
				houseDefault: "出租",
				imgList: [],
				imgUrlList: [],
				districtArray: [{ name: '城厢区' }, { name: '涵江区' }, { name: '荔城区' }, { name: '秀屿区' }, { name: '仙游县' }],
				districtIndex: 0,
			};
		},
		methods: {
			handleSelect(res) {
				this.imgList.push(res.tempFilePaths[0]);
				this.house.pic = this.imgList;
			},
			back() {
				uni.navigateBack()
			},
			houseChange(e) {
				console.log(e);
				let index = e.target.value;
				this.houseDefault = this.houseRange[index];
				this.house.type = index
			},
		  saveChange() {
				if (!this.checkHouseInfo()) {
					return;
				}
				// 如果有上传图片先上传图片再保存
				if (this.imgList.length > 0) {
					this.$request.uploadCurrentImage(this.imgList).then(res=>{
						this.house.pic = JSON.stringify(res);
						this.$request.request({
							url: "/house/saveChange",
							method: "POST",
							data: this.house,
						}).then(res=>{
							console.log(res);
							if (res.code == 401) {
								uni.showModal({
									title: res.message + "，是否跳转登录界面",
									cancelText:'否',
									confirmText:'是',
									success(res){
									  if(res.cancel){
									    
									  }else if(res.confirm){
									    uni.navigateTo({
									    	url: "../user/login"
									    })
										}
									}
								})
							} else if (res.code == 200) {
								uni.showToast({
									title: "修改成功，跳转中...",
									icon: "none",
									duration: 2000
								});
								setTimeout(function() {
								  uni.navigateBack()
								}, 1500);
							}
						});
					})
				} else {
					this.house.pic = JSON.stringify(this.house.pic)
					this.$request.request({
						url: "/house/saveChange",
						method: "POST",
						data: this.house,
					}).then(res=>{
						console.log(res);
						if (res.code == 401) {
							uni.showModal({
								title: res.message + "，是否跳转登录界面",
								cancelText:'否',
								confirmText:'是',
								success(res){
								  if(res.cancel){
								    
								  }else if(res.confirm){
								    uni.navigateTo({
								    	url: "../user/login"
								    })
									}
								}
							})
						} else if (res.code == 200) {
							uni.showToast({
								title: "修改成功，跳转中...",
								icon: "none",
								duration: 2000
							});
							setTimeout(function() {
							  uni.navigateBack()
							}, 1500);
						}
					});
				}
				
				//没有发布图片就直接对其他修改保存
				
			},
			checkHouseInfo() {
				// 标题
				if (this.house.title == "") {
					uni.showToast({
						title: "请填写标题",
						icon: "none"
					})
					return false;
				}
				if (this.house.title.length > 20) {
					uni.showToast({
						title: "标题过长",
						icon: "none"
					})
					return false;
				}
				
				// 描述
				if (this.house.introduce == "") {
					uni.showToast({
						title: "请填写描述",
						icon: "none"
					})
					return false;
				}
				if (this.house.introduce.length > 120) {
					uni.showToast({
						title: "描述过长",
						icon: "none"
					})
					return false;
				}
				
				// 房间数
				if (this.house.room == "") {
					uni.showToast({
						title: "请填写房间数",
						icon: "none"
					})
					return false;
				}
				if (this.house.room < 0 || this.house.room > 99) {
					uni.showToast({
						title: "请正确填写房间数",
						icon: "none"
					})
					return false;
				}
				
				// 客厅数
				if (this.house.parlor == "") {
					uni.showToast({
						title: "请填写客厅数",
						icon: "none"
					})
					return false;
				}
				if (this.house.parlor < 0 || this.house.parlor > 99) {
					uni.showToast({
						title: "请正确填写客厅数",
						icon: "none"
					})
					return false;
				}
				
				// 厕所数
				if (this.house.lavatory == "") {
					uni.showToast({
						title: "请填写厕所数",
						icon: "none"
					})
					return false;
				}
				if (this.house.lavatory < 0 || this.house.lavatory > 99) {
					uni.showToast({
						title: "请正确填写厕所数",
						icon: "none"
					})
					return false;
				}
				
				// 地址
				if (this.house.address == "") {
					uni.showToast({
						title: "请填写房源详细地址",
						icon: "none"
					})
					return false;
				}
				if (this.house.address.length > 25) {
					uni.showToast({
						title: "详细地址过长",
						icon: "none"
					})
					return false;
				}
				
				// 区
				if (this.house.district == "") {
					uni.showToast({
						title: "请填写房源详细地址",
						icon: "none"
					})
					return false;
				} else {
					if (this.house.district == '城厢区' || this.house.district == '涵江区' || this.house.district == '荔城区'
						|| this.house.district == '秀屿区' || this.house.district == '仙游县') {
								
					}
					else {
						uni.showToast({
							title: "请正确填写莆田市内区划",
							icon: "none"
						})
						return false;
					}
				}
				
				// 价格
				if (this.house.price < 0 || this.house.price > 100000000) {
					uni.showToast({
						title: "请正确填写价格",
						icon: "none"
					})
					return false;
				}
				
				return true;
			},
			// 将当前选择的所有图片上传到后台
			uploadCurrentImage() {
				for (let i=0; i<this.imgList.length; i++) {
					this.$request.fileUploadImage(this.imgList[i]).then(fileRes => {
						fileRes = JSON.parse(fileRes)
						this.imgUrlList.push(fileRes.data.urls[0].url);
					});
				}
				this.house.pic = JSON.stringify(this.imgUrlList);
			},
			checkNum(e) {
			  //正则表达试
			  e.target.value = (e.target.value.match(/^\d*(\.?\d{0,2})/g)[0]) || null
			  //重新赋值给input
			  this.$nextTick(() => {
					this.house.price= e.target.value
					})
			},
			handleDelete(err) { // 删除图片
			    const num = this.filePathsList.findIndex(v => v.url === err.tempFilePath);
			    this.filePathsList.splice(num, 1);
					this.imgList = [];
			},
			getHouseDetail() {
				this.$request.request({
					url: "/house/detail",
					method: "GET",
					data: {
						id: this.house.id
					}
				}).then(res=>{
					if (res.code == 200) {
						this.house = res.data
						this.house.picShow = JSON.parse(this.house.pic)
						this.house.pic = JSON.parse(this.house.pic)
						for (let i=0; i<this.house.picShow.length; i++) {
							this.house.picShow[i] = config.IMG_URL + this.house.picShow[i];
						}
						if (this.house.type == 1) {
							this.houseDefault = "出售"
						}
						for (let i=0; i < this.districtArray.length; i++) {
							if (this.districtArray[i].name == this.house.district) {
								this.districtIndex = i;
								break;
							}
						}
					} else if (res.code == 401) {
						uni.navigateTo({
							url: "../user/login"
						})
					}
				})
			},
			deleteHouse() {
				var that = this;
				uni.showModal({
					title: "删除后无法恢复，是否继续",
					success(res) {
						if (res.confirm) {
							that.$request.request({
								url: "/house/delete",
								method: "POST",
								data: that.house.id
							}).then(res=>{
								uni.showToast({
									title: res.message,
									icon: "none",
									duration: 2000
								})
								setTimeout(function() {
									uni.navigateBack();
								}, 1500)
							})
						}
					}
				})
			},
			display() {
				this.$request.request({
					url: "/house/display",
					method: "POST",
					data: this.house.id
				}).then(res=>{
					uni.showToast({
						title: res.message,
						icon: "none",
						duration: 2000
					})
					if (res.code == 200) {
						this.house.isDisplay = true;
					}
				})
			},
			cancelDisplay() {
				this.$request.request({
					url: "/house/cancelDisplay",
					method: "POST",
					data: this.house.id
				}).then(res=>{
					uni.showToast({
						title: res.message,
						icon: "none",
						duration: 2000
					})
					if (res.code == 200) {
						this.house.isDisplay = false;
					}
				})
			},
			districtPickerChange(e) {
				this.districtIndex = e.detail.value;
				this.house.district = this.districtArray[this.districtIndex].name;
			},
		},
		onLoad(option) {
			this.house.id = option.id;
			this.getHouseDetail();
		},
		onShow() {
		}
	}
</script>

<style lang="scss" scoped>
	.publish-house-page {
		
		.publish-item-group {
			background-color: #FFFFFF;
			margin: 10rpx 10rpx;
			padding: 30rpx 20rpx;
			border-radius: 20rpx;
			
			.item-group {
				border-bottom: #f2f1f6 1rpx solid;
				
				.item-input-group {
					padding: 20rpx 0;
					
					.item-input {
						width: 100%;
						font-size: 16px;
						color: #000000;
						text-align: justify;
					}
				}
				
				.item-bar {
					font-size: 18px;
					margin-top: 10rpx;
				}
				
				.imgs {
					margin-top: 10rpx;
				}
			}
			
			.room-title {
				width: 100%;
				display: inline-flex;
				font-size: 18px;
				padding: 10px 0;
				
				.room-title-text {
					flex: 1;
				}
			}
			
			.room-info {
				display: inline-flex;
				
				.room-item {
					flex: 1;
					display: inline-flex;
					
					.item-input {
						width: 50%;
						text-align: center;
					}
					
					.item-text {
						width: 50%;
						font-size: 16px;
						color: #000000;
						
					}
				}
				
			}
			
		}
		
	}

</style>
